import type { UploadRawFile } from 'element-plus';

export async function fileToBlob(file: UploadRawFile | undefined): Promise<Blob> {
  if (!file) {
    throw new Error('文件为空');
  }

  return new Promise<Blob>((resolve, reject) => {
    const reader = new FileReader();

    reader.onload = () => {
      const blob = new Blob([reader.result as ArrayBuffer], { type: file.type });
      resolve(blob);
    };

    reader.onerror = (error) => {
      reject(error);
    };

    reader.readAsArrayBuffer(file);
  });
}
